package com.ay.wiki_plt.service.Impl;

import com.ay.wiki_plt.common.BusinessExceptionCode;
import com.ay.wiki_plt.common.PageBean;
import com.ay.wiki_plt.domain.User;
import com.ay.wiki_plt.domain.dto.UserLoginReq;
import com.ay.wiki_plt.domain.dto.UserResetPasswordReq;
import com.ay.wiki_plt.domain.dto.UserSaveReq;
import com.ay.wiki_plt.domain.vo.UserLoginResp;
import com.ay.wiki_plt.exception.BusinessException;
import com.ay.wiki_plt.mapper.UserMapper;
import com.ay.wiki_plt.service.UserService;
import com.ay.wiki_plt.utils.CopyUtil;
import com.ay.wiki_plt.utils.SnowFlake;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SnowFlake snowFlake;

    /*查询全部用户*/
    @Override
    public PageBean list(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page, pageSize);
        List<User> userList = userMapper.list(name);
        Page<User> userPage = (Page<User>) userList;
        return new PageBean(userPage.getTotal(), userPage.getResult());
    }

    /*保存和修改接口*/
    @Override
    public void save(UserSaveReq req) {
        User user = CopyUtil.copy(req, User.class);
        Long userId = user.getId();
        if (userId == null) {

            if (ObjectUtils.isEmpty(selectByLoginName(user.getLoginName()))) {
                //新增

                user.setId(snowFlake.nextId());

                userMapper.insertUser(user);
            } else {
                //用户名已存在
                throw new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);
            }
        } else {
            //修改
            //不更改用户名
            user.setLoginName(null);
            //不修改密码
            user.setPassword(null);
            userMapper.updateUser(user);

        }
    }


    /*批量删除接口*/
    @Override
    public void deleteBath(List<Long> ids) {
        userMapper.deleteBatchUser(ids);
    }

    /*重置密码*/
    @Override
    public void resetPassword(UserResetPasswordReq req) {
        User user = CopyUtil.copy(req, User.class);
        userMapper.updatePassword(user);
    }


    /*根据用户名查询用户是否存在*/
    public User selectByLoginName(String LoginName) {
        return userMapper.selectByLoginName(LoginName);
    }


    /*登录*/
    @Override
    public UserLoginResp login(UserLoginReq req) {
        //对用户名进行查询
        User userDb = selectByLoginName(req.getLoginName());
        if (ObjectUtils.isEmpty(userDb)) {
            //用户名不存在
            log.info("用户名不存在:{}", req.getLoginName());
            throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERR);
        } else {
            //进行密码(加密密文比对)比对
            if (userDb.getPassword().equals(req.getPassword())) {
                //登录成功
                //UserLoginResp userLoginResp = CopyUtil.copy(userDb,UserLoginResp.class);
                //return userLoginResp;
                return CopyUtil.copy(userDb, UserLoginResp.class);
            } else {
                //密码不对
                log.info("密码错误，输入密码:{},数据库密码:{}", req.getPassword(), userDb.getPassword());
                throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERR);
            }

        }
    }

}
